package com.skivingcloud.admin.sys.service.impl;

import com.skivingcloud.admin.sys.entity.SysBusinessDesigner;
import com.skivingcloud.admin.sys.entity.SysFormDesigner;
import com.skivingcloud.admin.sys.mapper.SysBusinessDesignerMapper;
import com.skivingcloud.admin.sys.mapper.SysFormDesignerMapper;
import com.skivingcloud.admin.sys.service.ISysBusinessDesignerService;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.UuidUtil;

import net.sf.json.JSONObject;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.util.Arrays;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 应用设计记录 服务实现类
 * </p>
 *
 * @author skivingcloud
 * @since 2022-07-04
 */
@Service
public class SysBusinessDesignerServiceImpl extends ServiceImpl<SysBusinessDesignerMapper, SysBusinessDesigner> implements ISysBusinessDesignerService {

	@Autowired
	private SysFormDesignerMapper sysFormDesignerMapper;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		PageUtils pageUtils = PageUtils.getPageInfo(params);
		Page<SysBusinessDesigner> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
		SysBusinessDesigner sysBusinessDesigner = (SysBusinessDesigner) JSONObject.toBean(
				JSONObject.fromObject(params.get("dataForm")),
				SysBusinessDesigner.class);
		QueryWrapper<SysBusinessDesigner> queryWrapper = new QueryWrapper<>();
		if (StringUtils.isNotBlank(sysBusinessDesigner.getDesignerCode())) {
			queryWrapper.like("designer_code", "%" + sysBusinessDesigner.getDesignerCode() + "%");
		}
		if (StringUtils.isNotBlank(sysBusinessDesigner.getDesignerName())) {
			queryWrapper.like("designer_name", "%" + sysBusinessDesigner.getDesignerName() + "%");
		}
		if (StringUtils.isNotBlank(sysBusinessDesigner.getTypeCode())) {
			queryWrapper.like("type_code", "%" + sysBusinessDesigner.getTypeCode() + "%");
		}
		if (StringUtils.isNotBlank(sysBusinessDesigner.getHasTask())) {
			queryWrapper.eq("has_task", sysBusinessDesigner.getHasTask());
		}
		if (StringUtils.isNotBlank(sysBusinessDesigner.getTaskDefCode())) {
			queryWrapper.like("task_def_code", "%" + sysBusinessDesigner.getTaskDefCode() + "%");
		}
		this.baseMapper.selectList(queryWrapper);
		pageUtils = new PageUtils(p);
		return pageUtils;
	}

	@Override
	public void add(SysBusinessDesigner sysBusinessDesigner) {
		sysBusinessDesigner.setId(UuidUtil.get32UUID());
		sysBusinessDesigner.setVersion(1);
		this.baseMapper.insert(sysBusinessDesigner);
	}

	@Override
	public void update(SysBusinessDesigner sysBusinessDesigner) {
		this.baseMapper.updateById(sysBusinessDesigner);
	}

	@Override
	public void deleteBatch(String[] ids) {
		this.baseMapper.deleteBatchIds(Arrays.asList(ids));
	}

	@Override
	public SysBusinessDesigner selectById(String id) {
		return this.baseMapper.selectById(id);
	}

	/**
	 * 根据设计编码获取新增表单的设计json
	 * 
	 * @param designerCode
	 * @return
	 */
	@Override
	public JsonNode getFormJsonAddByBusDesignerCode(String designerCode) {
		QueryWrapper<SysBusinessDesigner> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("designer_code", designerCode);
		SysBusinessDesigner sysBusinessDesigner = this.baseMapper.selectOne(queryWrapper);
		if(sysBusinessDesigner != null){
			SysFormDesigner sysFormDesigner = sysFormDesignerMapper.selectById(sysBusinessDesigner.getNewFormId());
			if(sysFormDesigner != null){
				return sysFormDesigner.getFormDesJson();
			}
		}
		return null;
	}

	/**
	 * 根据设计编码获取包含表单json的设计
	 * 
	 * @param designerCode
	 * @return
	 */
	@Override
	public SysBusinessDesigner getSysBusinessDesignerByCodeHasJson(String designerCode) {
		QueryWrapper<SysBusinessDesigner> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("designer_code", designerCode);
		SysBusinessDesigner sysBusinessDesigner = this.baseMapper.selectOne(queryWrapper);
		if (sysBusinessDesigner != null) {
			SysFormDesigner sysFormDesignerNew = sysFormDesignerMapper.selectById(sysBusinessDesigner.getNewFormId());
			if (sysFormDesignerNew != null) {
				sysBusinessDesigner.setNewFormJson(sysFormDesignerNew.getFormDesJson());
			}
			SysFormDesigner sysFormDesignerMod = sysFormDesignerMapper.selectById(sysBusinessDesigner.getModFormId());
			if (sysFormDesignerMod != null) {
				sysBusinessDesigner.setModFormJson(sysFormDesignerMod.getFormDesJson());
			}
			SysFormDesigner sysFormDesignerDetail = sysFormDesignerMapper.selectById(sysBusinessDesigner.getDetailFormId());
			if (sysFormDesignerDetail != null) {
				sysBusinessDesigner.setDetailFormJson(sysFormDesignerDetail.getFormDesJson());
			}
		}
		return sysBusinessDesigner;
	}
}
